<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>

</head>

<body>
    <button onclick="send()">defaults</button>
    <button onclick="getRepo()">get repo</button>
    <script>
        // 配置基础路径和超时时间
        // axios.defaults.baseURL = 'http://localhost:3000'
        // axios.defaults.timeout = 2000

        // 配置多个基础路径
        const computerReq = axios.create({
            baseURL: 'http://localhost:3000',
            timeout: 20000
        })

        // 配置请求拦截器：一般用来携带公共参数
        computerReq.interceptors.request.use(config => {
            let token = localStorage.getItem('token')
            if (token) {
                config.headers = {
                    token
                }
            }
            return config
        })

        const gitReq = axios.create({
            baseURL: 'https://api.github.com',
            timeout: 20000
        })

        // 响应拦截器配置 - 简化响应结果 最多的

        computerReq.interceptors.response.use(response => {
            return response.data
        })

        function send() {
            computerReq.get('/computer').then(data => {
                console.log('data： ', data)
            })
        }

        gitReq.interceptors.response.use(response => response.data)

        function getRepo() {
            gitReq.get('/search/repositories', {
                params: {
                    'q': 're',
                    'sort': 'stars'
                }
            }).then(data => {
                console.log(data)
            })
        }


    </script>
</body>

</html>